<?php
if(!defined('BASEPATH'))
	exit('No direct script access allowed');

class Matches extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index() {
		$query = $this -> db -> query('SELECT username, password, full_name, contact_phone FROM users');

		foreach($query->result() as $row) {
			echo $row -> username;
			echo $row -> password;
			echo $row -> full_name;
			echo $row -> contact_phone;
		}

		echo 'Total Results: ' . $query -> num_rows();
		$this -> load -> view('matches_view');
	}

	public function submit() {

		$time = strtotime($_POST['match_time']);

		$timestamp = date('Y-m-d H:i:s', $time);

		echo $timestamp . "<br>";

		$data = array('match_date' => $timestamp, 'tournament_id' => $_POST['tournament_id'], 'team_visit' => $_POST['team-visit'], 'team_home' => $_POST['team-home']);

		$this -> db -> insert('matches', $data);

		//$torneo_id = $this -> db -> insert_id(); TODO create menu to keep adding matches or edit matches on another view

		$data['url'] = "matches/create/" . $_POST['tournament_id'];
		$this -> load -> view('success', $data);

		//print_r($_POST);

	}

	public function lista($tournamentid =FALSE) {

		$this -> load -> model('Torneo_Model');

		if($tournamentid) {

			$results = $this -> Torneo_Model -> get_tournament_info($tournamentid);

			//echo ":Size of the array is :" . sizeof($results);

			if(sizeof($results) > 0) {

				$data['category_name'] = $results[0] -> category_desc;
				$data['category_sport'] = $results[0] -> category_sport;
				$data['tournament_name'] = $results[0] -> tournament_name;
				$data['tournament_id'] = $results[0] -> tournament_id;
				$data['tournament_flag'] = TRUE;

				$matches = $this -> Torneo_Model -> get_matches_for_tournament($tournamentid);

				$data['matches'] = $matches;

				//print_r($matches);

				/*
				 $teams = $this -> Torneo_Model -> get_tournament_teams($tournamentid);

				 $teamoptions = array();

				 foreach($teams as $row) {

				 $teamoptions[$row->team_id] = $row->team_name;

				 }

				 //print_r($teams);
				 /*print_r($results);
				 echo "<br>";
				 echo $results[0] -> tournament_id . "<br>";
				 echo $results[0] -> category_desc . "<br>";
				 echo $results[0] -> category_sport . "<br>";
				 echo $results[0] -> tournament_name . "<br>";

				 $data['teams'] = $teamoptions;

				 $data['teamsjs'] = 'id="team-local"';

				 $data['category_name'] = array('name' => 'category_name', 'id' => 'category_name', 'readonly' => 'readonly', 'value' => $data['category_name'] . " - " . $data['category_sport']);
				 $data['tournament_id'] = array('name' => 'tournament_id', 'id' => 'tournament_id', 'type' => 'hidden', 'value' => $data['tournament_id']);
				 $data['tournament_name'] = array('name' => 'tournament_name', 'id' => 'tournament_name', 'readonly' => 'readonly', 'value' => $data['tournament_name']);
				 $this -> load -> view('matches_create_view', $data);
				 * */
				$this -> load -> view('match_list_view', $data);

			} else {

				$data['action'] = "Listado de encuentros";
				$data['message'] = "El Identificador del torneo es erroneo.";
				$this -> load -> view('error_view', $data);
			}
		} else {

			$matches = $this -> Torneo_Model -> get_all_matches();

			$data['tournament_flag'] = FALSE;
			$data['matches'] = $matches;

			//print_r($matches);

			$this -> load -> view('match_list_view', $data);

			/*
			 $data['action'] = "Crear Juego";
			 $data['message'] = "Identificador del torneo no fue proporcionado o es erroneo.";
			 $this -> load -> view('error_view', $data);*/

		}

	}

	public function create($tournamentid =FALSE) {

		if($tournamentid) {

			$this -> load -> model('Torneo_Model');

			$results = $this -> Torneo_Model -> get_tournament_info($tournamentid);

			if(sizeof($results) > 0) {

				$data['category_name'] = $results[0] -> category_desc;
				$data['category_sport'] = $results[0] -> category_sport;
				$data['tournament_name'] = $results[0] -> tournament_name;
				$data['tournament_id'] = $results[0] -> tournament_id;

				$teams = $this -> Torneo_Model -> get_tournament_teams($tournamentid);

				$teamoptions = array();

				foreach($teams as $row) {

					$teamoptions[$row -> team_id] = $row -> team_name;

				}

				$data['teams'] = $teamoptions;
				$data['teamsjs'] = 'id="team-local"';
				$data['category_name'] = array('name' => 'category_name', 'id' => 'category_name', 'readonly' => 'readonly', 'value' => $data['category_name'] . " - " . $data['category_sport']);
				$data['tournament_id'] = array('name' => 'tournament_id', 'id' => 'tournament_id', 'type' => 'hidden', 'value' => $data['tournament_id']);
				$data['tournament_name'] = array('name' => 'tournament_name', 'id' => 'tournament_name', 'readonly' => 'readonly', 'value' => $data['tournament_name']);
				$this -> load -> view('matches_create_view', $data);

			} else {

				$data['action'] = "Crear Juego";
				$data['message'] = "Identificador del torneo no fue proporcionado o es erroneo.";
				$this -> load -> view('error_view', $data);

			}
		} else {

			$data['action'] = "Crear Juego";
			$data['message'] = "Identificador del torneo no fue proporcionado o es erroneo.";
			$this -> load -> view('error_view', $data);

		}

	}

	public function edit($match_id =FALSE) {

		$this -> load -> model('Torneo_Model');

		if($match_id && is_numeric($match_id)) {

			$results = $this -> Torneo_Model -> get_match_info_for_edit($match_id);

			if(sizeof($results) > 0) {

				if($results[0] -> match_scored) {

					$results = $this -> Torneo_Model -> get_score_by_match_id($match_id);

					$data['match_id'] = $results[0] -> match_id;
					$data['match_date'] = $results[0] -> match_date;
					$data['match_scored'] = $results[0] -> match_scored;
					$data['team_home_id'] = $results[0] -> team_home_id;
					$data['team_home_name'] = $results[0] -> team_home_name;
					$data['team_visit_id'] = $results[0] -> team_visit_id;
					$data['team_visit_name'] = $results[0] -> team_visit_name;
					$data['home_score'] = $results[0] -> home_score;
					$data['visit_score'] = $results[0] -> visit_score;

					//

					$annotations_home = $this -> Torneo_Model -> get_annotations_by_match_id($match_id, $data['team_home_id']);
					$annotations_visit = $this -> Torneo_Model -> get_annotations_by_match_id($match_id, $data['team_visit_id']);

					$data['annotations_home'] = $annotations_home;
					$data['annotations_visit'] = $annotations_visit;

					$this -> load -> view('match_user_view', $data);
					return ;

				}

				$data['team_home_name'] = $results[0] -> team_home;
				$data['team_home_id'] = $results[0] -> team_home_id;
				$data['team_visit_id'] = $results[0] -> team_visit_id;
				$data['team_visit_name'] = $results[0] -> team_visit;

				$time = strtotime($results[0] -> match_date);

				$timestamp = date('d-m-Y h:i A', $time);
				
				$timestamp2 = date('m/d/Y h:i A', $time);

				//echo $timestamp . "<br>";

				$data['match_date'] = $timestamp;
				
				$data['match_date_format'] = $timestamp2;
				

				$data['match_ide'] = $match_id;

				$results_players = $this -> Torneo_Model -> get_players_in_team($data['team_home_id']);

				$players_home = array();

				foreach($results_players as $row) {

					$players_home[$row -> player_id] = $row -> player_name;

				}

				$data['players_home'] = $players_home;

				$players_visit = array();

				$results_players = $this -> Torneo_Model -> get_players_in_team($data['team_visit_id']);

				foreach($results_players as $row) {

					$players_visit[$row -> player_id] = $row -> player_name;

				}

				$data['players_visit'] = $players_visit;

				$data['match_id'] = array('name' => 'match_id', 'id' => 'match_id', 'type' => 'hidden', 'value' => $match_id);
				$data['team_home_id'] = array('name' => 'team_home_id', 'id' => 'team_home_id', 'type' => 'hidden', 'value' => $data['team_home_id']);
				$data['team_visit_id'] = array('name' => 'team_visit_id', 'id' => 'team_visit_id', 'type' => 'hidden', 'value' => $data['team_visit_id']);

				//$data['match_info'] = $results[0]->team_home;
				/*
				 $data['category_name'] = $results[0] -> category_desc;
				 $data['category_sport'] = $results[0] -> category_sport;
				 $data['tournament_name'] = $results[0] -> tournament_name;
				 $data['tournament_id'] = $results[0] -> tournament_id;
				 */

				//print_r($data);

				$this -> load -> view('edit_match_view', $data);

			}

		} else {
			$data['action'] = "Editar Juego";
			$data['message'] = "Identificador del torneo no fue proporcionado o es erroneo.";
			$this -> load -> view('error_view', $data);

		}
	}

	public function save() {

		print_r($_POST);

		$data['match_scored'] = true;
		$this -> db -> where('match_id', $_POST['match_id']);
		$this -> db -> update('matches', $data);

		$data['url'] = "matches/edit/" . $_POST['match_id'];
		$this -> load -> view('success', $data);

	}

	public function savescore() {

		/*
		 *
		 * TODO add rules to sum points and change values
		 * Array
		 (
		 [match_id] => 5
		 [player_id] => 29
		 [team_id] => 7
		 [time] =>
		 )
		 *
		 * */

		$data = array('match_id' => $_POST['match_id'], 'player_id' => $_POST['player_id'], 'team_id' => $_POST['team_id'], 'time' => $_POST['time'], 'points' => 1);
		$this -> db -> insert('player_score', $data);
		//print_r($_POST);
		echo "Success!";

		print_r($_POST);

	}

	public function view($match_id =FALSE) {

		$this -> load -> model('Torneo_Model');

		if($match_id && is_numeric($match_id)) {

			$results = $this -> Torneo_Model -> get_score_by_match_id($match_id); 
			
			//print_r($results);

			if(sizeof($results) > 0) {

				$data['match_id'] = $results[0] -> match_id;
				$data['match_date'] = $results[0] -> match_date;
				$data['match_scored'] = $results[0] -> match_scored;
				$data['team_home_id'] = $results[0] -> team_home_id;
				$data['team_home_name'] = $results[0] -> team_home_name;
				$data['team_visit_id'] = $results[0] -> team_visit_id;
				$data['team_visit_name'] = $results[0] -> team_visit_name;
				$data['home_score'] = $results[0] -> home_score;
				$data['visit_score'] = $results[0] -> visit_score;

				//

				$annotations_home = $this -> Torneo_Model -> get_annotations_by_match_id($match_id, $data['team_home_id']);
				$annotations_visit = $this -> Torneo_Model -> get_annotations_by_match_id($match_id, $data['team_visit_id']);

				$data['annotations_home'] = $annotations_home;
				$data['annotations_visit'] = $annotations_visit;

				$this -> load -> view('match_user_view', $data);
				return ;

			}

		} else {

			$data['action'] = "Ver Encuentro";
			$data['message'] = "Identificador del encuentro no fue proporcionado o es erroneo.";
			$this -> load -> view('error_view', $data);

		}

	}

	public function scorematchajax($match_id =FALSE) {

		$this -> load -> model('Torneo_Model');

		if($match_id && is_numeric($match_id)) {

			$results = $this -> Torneo_Model -> get_score_by_match_id($match_id);

			$data['match_id'] = $results[0] -> match_id;
			$data['match_date'] = $results[0] -> match_date;
			$data['match_scored'] = $results[0] -> match_scored;
			$data['team_home_id'] = $results[0] -> team_home_id;
			$data['team_home_name'] = $results[0] -> team_home_name;
			$data['team_visit_id'] = $results[0] -> team_visit_id;
			$data['team_visit_name'] = $results[0] -> team_visit_name;
			$data['home_score'] = $results[0] -> home_score;
			$data['visit_score'] = $results[0] -> visit_score;

			//

			$annotations_home = $this -> Torneo_Model -> get_annotations_by_match_id($match_id, $data['team_home_id']);
			$annotations_visit = $this -> Torneo_Model -> get_annotations_by_match_id($match_id, $data['team_visit_id']);

			$data['annotations_home'] = $annotations_home;
			$data['annotations_visit'] = $annotations_visit;

			//print_r($data);
			$this -> load -> view('score_ajax_view', $data);

		} else {

			echo "<br /> Error al mostrar puntos del encuentro<br />";

		}

	}

	public function deleteScore() {

		$this -> db -> delete('player_score', array('score_id' => $_POST['score_id']));
		echo "Score deleted!";

	}


	public function modifydate(){
		
		$time = strtotime($_POST['match_time']);

		$timestamp = date('Y-m-d H:i:s', $time);

		echo $timestamp . "<br>";
		
		print_r($_POST);
		
		$data['match_date'] = $timestamp;
		
		
		
		$this -> db -> where('match_id', $_POST['match_id']);
		$this -> db -> update('matches', $data);

		//$data['url'] = "matches/edit/" . $_POST['match_id'];
		//$this -> load -> view('success', $data);
		
		
	}

}

/* End of file matches.php */
/* Location: ./application/controllers/matches.php */
